package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.cmd.orewar.task.GetRewardTask;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_GET_REWARD, desc = "领取占领收益")
public class OreGetRewardCmd extends AbstractCommand {

	private int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		OreWarProto.OreGetRewardReqMsg reqMsg = OreWarProto.OreGetRewardReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		ActivityInfo activityInfo = OreWarActivityMgr.getInTimeActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!OreWarMgr.isActivityActive(activityId)){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_DAY_TIME;
			return;
		}
		if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
			OreWarMgr.addTask(activityId,new GetRewardTask(packet, player));
		}else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
			OreWarProto.CrossOreGetRewardReqMsg.Builder msg = OreWarProto.CrossOreGetRewardReqMsg.newBuilder();
			msg.setActivityId(reqMsg.getActivityId());
			int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
			GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_OREWAR_GET_REWARD, msg,player.getUserId(),activityId,groupId);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			OreWarProto.OreGetRewardRespMsg.Builder respMsg = OreWarProto.OreGetRewardRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_ORE_WAR_GET_REWARD, respMsg);
		}
	}
}
